/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author Thanh
 */
public class Server {

    private String username;
    private String password;
    private String url;

    public Server() {
        getServerConnectionInfo();
    }

    public String getPassword() {
        return password;
    }

    public String getUrl() {
        return url;
    }

    public String getUsername() {
        return username;
    }

    private void getServerConnectionInfo() {
        FileInputStream inputFile = null;
        try {
            XMLInputFactory xif = XMLInputFactory.newInstance();
            File file = new File("/src/java/utilities/webConfig.xml");
            inputFile = new FileInputStream(file);
            XMLStreamReader reader = xif.createXMLStreamReader(inputFile);
            while(reader.hasNext())
            {
                int cursor=0;
                cursor = reader.next();
                if(cursor == XMLStreamConstants.START_ELEMENT)
                {
                    if(reader.getLocalName().equals("server"))
                    {
                        this.username = reader.getAttributeValue("", "username");
                        this.password = reader.getAttributeValue("", "password");
                        this.url = reader.getAttributeValue("", "url");
                    }
                }
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                inputFile.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();;
            }
        }
    }
}
